<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/01/05 20:57:52 $)
// $RCSfile: functions_mobile.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* Dispatch Ticket XML
*/
function dispatchMobileXML($ticketviewid, $departmentidlist, $ticketstatusidlist, $_tree, &$xml)
{
	global $dbCore, $_SWIFT, $datastore, $_fieldpointer, $template;

	ob_start();

	$_ticketview = &$_SWIFT["ticketviewcache"][$ticketviewid];
	if (!is_array($_ticketview))
	{
		trigger_error($_SWIFT["language"]["invalidview"], E_USER_ERROR);
	}

	$appendurl = "&departmentid=". intval($_REQUEST["departmentid"]) ."&ticketstatusid=". intval($_REQUEST["ticketstatusid"]);
	$_grid = $_update = array();
	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];
	if (!is_array($_gridset))
	{
		$_gridset = array();
		$gridsetupdate = true;
	}

	$_staffid = &$_SWIFT["staff"]["staffid"];

	$fields = $_customfieldidlist = array();
	$_findex = 0;

	/**
	* ###############################################
	* BUILD THE QUERIES
	* ###############################################
	*/
	$ticketcount = 0;
	$_ticketidlist = array();
	$strdepartmentid = buildIN($departmentidlist);
	$strticketstatus = buildIN($ticketstatusidlist);
	$ojoinsql = array();
	unset($ownerjoin);

/*
	if ($_ticketview["viewalltickets"] == 1)
	{
		// No Owner Join
	} else {
		if ($_ticketview["viewunassigned"] == 1) {
			$ojoinsql[] = "0";
		}
		if ($_ticketview["viewassigned"] == 1)
		{
			$ojoinsql[] = $_SWIFT["staff"]["staffid"];
		}
	}
	
	if (count($ojoinsql))
	{
		$ownerjoin = " AND `ownerstaffid` IN (". buildIN($ojoinsql) .")";
	}*/

	$gridurl = buildURL("index.php?_m=". $_SWIFT["currentmodule"] ."&_a=". $_SWIFT["currentaction"].$appendurl);

	$totalfields = count($fields)+1;
	$colspan = $totalfields;

	// We build list of ticket attachments
	$_attachmentlist = $_tree["mobile"][0]["children"]["options"][0]["children"]["attachments"][0]["children"]["ticket"];
	//echo "Total Ticket Attachments: ".count($_attachmentlist)."\n";
	$_attachmentticketidlist = $_attachidlist = array();
	if (_is_array($_attachmentlist))
	{
		foreach ($_attachmentlist as $key=>$val)
		{
			if (!_is_array($_attachmentticketidlist))
			{
				$_attachmentticketidlist[] = intval($val["children"]["ticketid"][0]["values"][0]);
				//echo "Adding ticket to list: ".intval($val["children"]["ticketid"][0]["values"][0])."\n";
			} else {
				if (!in_array($val["children"]["ticketid"][0]["values"][0], $_attachmentticketidlist))
				{
					//echo "Adding ticket to list: ".intval($val["children"]["ticketid"][0]["values"][0])."\n";
					$_attachmentticketidlist[] = intval($val["children"]["ticketid"][0]["values"][0]);
				}
			}
		}
	}


	// ======= START GRID CONTENTS =======
	$dbCore->query("SELECT t.ticketid FROM `". TABLE_PREFIX ."tickets` AS t WHERE t.departmentid IN (". $strdepartmentid .") AND t.ticketstatusid IN (". $strticketstatus .")". $ownerjoin ." ORDER BY `ticketid` ASC;");
	while ($dbCore->nextRecord())
	{
		$_ticketidlist[] = $dbCore->Record["ticketid"];
		//echo "Sending Ticket ID: ".$dbCore->Record["ticketid"]."\n";
		if (_is_array($_attachmentticketidlist))
		{
			//echo "Array is valid\n";
			if (in_array($dbCore->Record["ticketid"], $_attachmentticketidlist))
			{
				//echo "Adding FINAL ticket to list: ".$dbCore->Record["ticketid"]."\n";
				$_attachidlist[] = $dbCore->Record["ticketid"];
			} else {
				//echo "Entry already exists\n";
			}
		}
	}

	$xml->addTag("ticketcount", count($_ticketidlist));

	$strticketid = buildIN($_ticketidlist);
	$_tquery = "SELECT t.* FROM `". TABLE_PREFIX ."tickets` AS t WHERE t.ticketid IN (". $strticketid .") ORDER BY `ticketid` ASC;";

	$dbCore->query($_tquery, 2);
	$cnt = 0;
	$_tickets = $_useridlist = $_usernotes = $_timelist = $_timeticketidlist = array();
	while ($dbCore->nextRecord(2))
	{
		$_tickets[$dbCore->Record2["ticketid"]] = $dbCore->Record2;
		$_tickets[$dbCore->Record2["ticketid"]]["object"] =& new TicketMain($dbCore->Record2["ticketid"], false);
		$_tickets[$dbCore->Record2["ticketid"]]["ticketposts"] = array();
		$_tickets[$dbCore->Record2["ticketid"]]["ticketnotes"] = array();
		$_tickets[$dbCore->Record2["ticketid"]]["timelist"] = array();
		$_tickets[$dbCore->Record2["ticketid"]]["attachments"] = array();
		$_tickets[$dbCore->Record2["ticketid"]]["totalworked"] = $_tickets[$dbCore->Record2["ticketid"]]["totalbilled"] = 0;

		if ($dbCore->Record2["hasbilling"] == 1)
		{
			$_timeticketidlist[] = $dbCore->Record2["ticketid"];
		}

		if (!empty($dbCore->Record2["userid"]) && !in_array($dbCore->Record2["userid"], $_useridlist))
		{
			$_useridlist[] = intval($dbCore->Record2["userid"]);
		}
	}

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketposts` WHERE `ticketid` IN (". $strticketid .") ORDER BY `ticketpostid` ASC;", 2);
	while ($dbCore->nextRecord(2))
	{
		$_tickets[$dbCore->Record2["ticketid"]]["ticketposts"][] = $dbCore->Record2;

		// TODO: ]]> replace with ""
	}

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketnotes` WHERE (`typeid` IN (". $strticketid .") AND `notetype` = '". NOTE_TICKET ."') OR (`typeid` IN (". buildIN($_useridlist) .") AND `notetype` = '". NOTE_USER ."') ORDER BY `dateline` ASC;", 2);
	while ($dbCore->nextRecord(2))
	{
		if ($dbCore->Record2["notetype"] == NOTE_USER)
		{
			$_usernotes[$dbCore->Record2["typeid"]][] = $dbCore->Record2;
		} else {
			$_tickets[$dbCore->Record2["typeid"]]["ticketnotes"][] = $dbCore->Record2;
		}
	}

	if (count($_timeticketidlist))
	{
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."tickettimetrack` WHERE `ticketid` IN (". buildIN($_timeticketidlist) .") ORDER BY `timetrackid` ASC;", 2);
		while ($dbCore->nextRecord(2))
		{
			$_tickets[$dbCore->Record2["ticketid"]]["timelist"][] = $dbCore->Record2;

			$_tickets[$dbCore->Record2["ticketid"]]["totalworked"] += intval($dbCore->Record2["timespent"]);
			$_tickets[$dbCore->Record2["ticketid"]]["totalbilled"] += intval($dbCore->Record2["timebillable"]);
		}
	}

	if (count($_attachidlist))
	{
		//echo "Building Attachment List: ".buildIN($_attachidlist)."\n";
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."attachments` WHERE `ticketid` IN (". buildIN($_attachidlist) .");");
		while ($dbCore->nextRecord())
		{
			$_attachment = $dbCore->Record;
			$_tickets[$dbCore->Record["ticketid"]]["attachments"][$dbCore->Record["attachmentid"]] = $dbCore->Record;

			// Is the attachment stored as a file?
			if ($_attachment["attachmenttype"] == ATTACHMENT_FILE)
			{
				// Yes, just read out the file
				$fp = fopen("./files/".$_attachment["storefilename"], "rb");
				if (!$fp)
				{
					continue;
				}

				while(!feof($fp)) {
					$_tickets[$dbCore->Record["ticketid"]]["attachments"][$dbCore->Record["attachmentid"]]["contents"] .= fread($fp, 8192);
					flush();
				}

				fclose($fp);


			} else if ($_attachment["attachmenttype"] == ATTACHMENT_DL && $module->isRegistered(MODULE_DOWNLOADS)) {

				$_download = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."downloaditems` WHERE `downloaditemid` = '". intval($_attachment["downloaditemid"]) ."';");
				if ($_download["downloaditemid"])
				{
					$fp = fopen("./files/".$_download["storedfilename"], "rb");
					if (!$fp)
					{
						continue;
					}

					while(!feof($fp)) {
						$_tickets[$dbCore->Record["ticketid"]]["attachments"][$dbCore->Record["attachmentid"]]["contents"] .= fread($fp, 8192);
					}

					fclose($fp);
				}

			} else if ($_attachment["attachmenttype"] == ATTACHMENT_DB) {
				// Attachment type is set to database, read the chunks into a temporary file
				$tempfile = "temp_".buildHash();

				$fp = fopen("./files/".$tempfile, "wb");
				if (!$fp)
				{
					continue;
				}

				$writecontents = "";

				$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."attachmentchunks` WHERE `attachmentid` = '". $dbCore->escape($_attachment["attachmentid"]) ."' ORDER BY `chunkid` ASC;", 3);
				while ($dbCore->nextRecord(3))
				{
					@fflush($fp);
					fwrite($fp, $dbCore->Record3["contents"]);
				}
				@fflush($fp);
				fclose($fp);
				unset($fp);

				// Now that we have a temporary file, we need to pump out the combined chunks to the user
				$fp = fopen("./files/".$tempfile, "rb");
				if (!$fp)
				{
					continue;
				}

				$_contents = "";
				while(!feof($fp)) {
					$_tickets[$dbCore->Record["ticketid"]]["attachments"][$dbCore->Record["attachmentid"]]["contents"] .= fread($fp, 8192);
				}

				fclose($fp);

				// Nuke the temporary file as we dont need it anymore
				unlink("./files/".$tempfile);
			}
		}
		//print_r($_tickets[$dbCore->Record["ticketid"]]["attachments"]);
	}

	foreach ($_tickets as $key=>$val)
	{
		unset($record);
		unset($bgrecord);
		$cnt++;
		$id = $val["ticketid"];

		$xml->addParentTag("ticket");

			$xml->addTag("entry", intval($val["ticketid"]), array("name" => "ticketid"));
			$xml->addTag("entry", iif($_SWIFT["settings"]["t_eticketid"]=="seq", intval($val["ticketid"]), $val["ticketmaskid"]), array("name" => "ticketdisplayid"));

			if ($val["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
			{
				// New Ticket
				$xml->addTag("entry", "1", array("name" => "isnew"));
			} else {
				// Normal Ticket
				$xml->addTag("entry", "0", array("name" => "isnew"));
			}

			$xml->addTag("entry", $val["departmentid"], array("name" => "departmentid"));
			$xml->addTag("entry", $val["ticketstatusid"], array("name" => "ticketstatusid"));
			$xml->addTag("entry", $val["priorityid"], array("name" => "priorityid"));
			$xml->addTag("entry", $val["emailqueueid"], array("name" => "emailqueueid"));
			$xml->addTag("entry", $val["userid"], array("name" => "userid"));
			$xml->addTag("entry", $val["ownerstaffid"], array("name" => "ownerstaffid"));
			$xml->addTag("entry", returnSanitizedMobileData($val["fullname"]), array("name" => "fullname"));
			$xml->addTag("entry", returnSanitizedMobileData($val["email"]), array("name" => "email"));
			$xml->addTag("entry", returnSanitizedMobileData($val["lastreplier"]), array("name" => "lastreplier"));
			$xml->addTag("entry", returnSanitizedMobileData($val["subject"]), array("name" => "subject"));
			$xml->addTag("entry", $val["dateline"], array("name" => "dateline"));
			$xml->addTag("entry", edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"]), array("name" => "displaydate"));
			$xml->addTag("entry", $val["lastactivity"], array("name" => "lastactivity"));
			$xml->addTag("entry", $val["laststaffreplytime"], array("name" => "laststaffreplytime"));
			$xml->addTag("entry", $val["slaplanid"], array("name" => "slaplanid"));
			$xml->addTag("entry", $val["ticketslaplanid"], array("name" => "ticketslaplanid"));
			$xml->addTag("entry", $val["duetime"], array("name" => "duetime"));
			$xml->addTag("entry", $val["totalreplies"], array("name" => "totalreplies"));
			$xml->addTag("entry", $val["ipaddress"], array("name" => "ipaddress"));
			$xml->addTag("entry", $val["flagtype"], array("name" => "flagtype"));
			$xml->addTag("entry", $val["hasnotes"], array("name" => "hasnotes"));
			$xml->addTag("entry", $val["hasattachments"], array("name" => "hasattachments"));
			$xml->addTag("entry", $val["isemailed"], array("name" => "isemailed"));
			$xml->addTag("entry", $val["edited"], array("name" => "edited"));
			$xml->addTag("entry", $val["editedbystaffid"], array("name" => "editedbystaffid"));
			$xml->addTag("entry", $val["editeddateline"], array("name" => "editeddateline"));

			if ($val["creator"] == POST_STAFF)
			{
				$_creator = "staff";
			} else if ($val["creator"] == POST_THIRDPARTY) {
				$_creator = "thirdparty";
			} else if ($val["creator"] == POST_RECIPIENT) {
				$_creator = "recipient";
			} else {
				$_creator = "client";
			}

			$xml->addTag("entry", $_creator, array("name" => "creator"));
			$xml->addTag("entry", $val["hasdraft"], array("name" => "hasdraft"));
			$xml->addTag("entry", $val["hasbilling"], array("name" => "hasbilling"));
			$xml->addTag("entry", $val["isphonecall"], array("name" => "isphonecall"));
			$xml->addTag("entry", $val["isescalated"], array("name" => "isescalated"));
			$xml->addTag("entry", $val["phoneno"], array("name" => "phoneno"));

			// Attachments
			foreach ($val["attachments"] as $attachkey=>$attachval)
			{
				$xml->addParentTag("attachment");
					$xml->addTag("filename", $attachval["filename"]);
					$xml->addTag("filesize", strlen($attachval["contents"]));
					$xml->addTag("md5", md5($attachval["contents"]));
					$xml->addTag("contents", base64_encode(gzcompress($attachval["contents"], iif(isset($_SWIFT["settings"]["cpu_winappcompresslevel"]), $_SWIFT["settings"]["cpu_winappcompresslevel"], 9))));
				$xml->endParentTag("attachment");
			}

			foreach ($val["ticketposts"] as $postkey=>$postval)
			{
				$xml->addParentTag("post");

					$xml->addTag("entry", $postval["ticketpostid"], array("name" => "ticketpostid"));
					$xml->addTag("entry", $postval["dateline"], array("name" => "dateline"));
					$xml->addTag("entry", edate($_SWIFT["settings"]["dt_datetimeformat"], $postval["dateline"]), array("name" => "displaydate"));
					$xml->addTag("entry", returnSanitizedMobileData($postval["fullname"]), array("name" => "fullname"));
					$xml->addTag("entry", returnSanitizedMobileData($postval["email"]), array("name" => "email"));
					$xml->addTag("entry", $postval["hasattachments"], array("name" => "hasattachments"));
					$xml->addTag("entry", $postval["edited"], array("name" => "edited"));
					$xml->addTag("entry", $postval["editedbystaffid"], array("name" => "editedbystaffid"));
					$xml->addTag("entry", $postval["editeddateline"], array("name" => "editeddateline"));

					if ($postval["creator"] == POST_STAFF)
					{
						$_creator = "staff";
					} else if ($postval["creator"] == POST_THIRDPARTY) {
						$_creator = "thirdparty";
					} else if ($postval["creator"] == POST_RECIPIENT) {
						$_creator = "recipient";
					} else {
						$_creator = "client";
					}

					$xml->addTag("entry", $_creator, array("name" => "creator"));
					$xml->addTag("entry", $postval["isemailed"], array("name" => "isemailed"));
					$xml->addTag("entry", $postval["staffid"], array("name" => "staffid"));
					$xml->addTag("entry", returnSanitizedMobileData($postval["contents"]), array("name" => "contents"));

				$xml->endTag("post");
			}

			if (_is_array($_usernotes[$val["userid"]]))
			{
				foreach ($_usernotes as $notekey=>$noteval)
				{
					$xml->addParentTag("note");
						$xml->addTag("entry", $noteval["ticketnoteid"], array("name" => "ticketnoteid"));
						$xml->addTag("entry", $noteval["dateline"], array("name" => "dateline"));
						$xml->addTag("entry", $_SWIFT["staffcache"][$noteval["bystaffid"]]["fullname"], array("name" => "fullname"));
						$xml->addTag("entry", returnSanitizedMobileData($noteval["notes"]), array("name" => "contents"));
						$xml->addTag("entry", "usernote", array("name" => "type"));
					$xml->endTag("note");
				}
			}

			foreach ($val["ticketnotes"] as $notekey=>$noteval)
			{
				$xml->addParentTag("note");
						$xml->addTag("entry", $noteval["ticketnoteid"], array("name" => "ticketnoteid"));
						$xml->addTag("entry", $noteval["dateline"], array("name" => "dateline"));
						$xml->addTag("entry", $_SWIFT["staffcache"][$noteval["bystaffid"]]["fullname"], array("name" => "fullname"));
						$xml->addTag("entry", returnSanitizedMobileData($noteval["notes"]), array("name" => "contents"));
						$xml->addTag("entry", "ticketnote", array("name" => "type"));
				$xml->endTag("note");
			}

			foreach ($val["timelist"] as $timekey => $timeval)
			{
				$xml->addParentTag("billing");
					$xml->addTag("entry", $timeval["timetrackid"], array("name" => "timetrackid"));
					$xml->addTag("entry", $timeval["dateline"], array("name" => "dateline"));
					$xml->addTag("entry", $_SWIFT["staffcache"][$timeval["creatorstaffid"]]["fullname"], array("name" => "creatorname"));
					$xml->addTag("entry", $timeval["timespent"], array("name" => "timespent"));
					$xml->addTag("entry", $timeval["timebillable"], array("name" => "timebillable"));
					$xml->addTag("entry", returnSanitizedMobileData($timeval["notes"]), array("name" => "notes"));
				$xml->endTag("billing");
			}

			// ======= Varun: CUSTOM FIELD DATA 1/6/2007 =======
			if (is_object($val["object"]) && !empty($val["object"]->ticket["ticketid"]))
			{
				$val["object"]->loadTicketCustomFields();

				if (_is_array($val["object"]->ticket["fields"]))
				{
					foreach ($val["object"]->ticket["fields"] as $fieldkey=>$fieldval)
					{
						$xml->addParentTag("customfield");
						unset($fieldvalue);
						if (_is_array($fieldval["fieldvalue"]))
						{
							foreach ($fieldval["fieldvalue"] as $fkey=>$fval)
							{
								$fieldvalue .= htmlspecialchars($fval)."\r\n";
							}
						} else {
							$fieldvalue = htmlspecialchars($fieldval["fieldvalue"]);
						}

						$xml->addTag("entry", $fieldval["title"], array("name" => "fieldtitle"));
						$xml->addTag("entry", $fieldvalue, array("name" => "fieldvalue"));
						$xml->endTag("customfield");
					}
				}
			}
			// ======= END CUSTOM FIELD DATA =======

		$xml->endTag("ticket");
	}

	return true;
}

/**
* Get the HEX Color Code
*/
function getMobileColorCode($color)
{
	$regresult = array();
	$_colorlist["aliceblue"] = "#f0f8ff";
	$_colorlist["antiquewhite"] = "#faebd7";
	$_colorlist["aqua"] = "#00ffff";
	$_colorlist["aquamarine"] = "#7fffd4";
	$_colorlist["azure"] = "#f0ffff";
	$_colorlist["beige"] = "#f5f5dc";
	$_colorlist["bisque"] = "#ffe4c4";
	$_colorlist["black"] = "#000000";
	$_colorlist["blanchedalmond"] = "#ffebcd";
	$_colorlist["blue"] = "#0000ff";
	$_colorlist["blueviolet"] = "#8a2be2";
	$_colorlist["brown"] = "#a52a2a";
	$_colorlist["burlywood"] = "#deb887";
	$_colorlist["cadetblue"] = "#5f9ea0";
	$_colorlist["chartreuse"] = "#7fff00";
	$_colorlist["chocolate"] = "#d2691e";
	$_colorlist["coral"] = "#ff7f50";
	$_colorlist["cornflowerblue"] = "#6495ed";
	$_colorlist["cornsilk"] = "#fff8dc";
	$_colorlist["crimson"] = "#dc143c";
	$_colorlist["cyan"] = "#00ffff";
	$_colorlist["darkblue"] = "#00008b";
	$_colorlist["darkcyan"] = "#008b8b";
	$_colorlist["darkgoldenrod"] = "#b8860b";
	$_colorlist["darkgray"] = "#a9a9a9";
	$_colorlist["darkgreen"] = "#006400";
	$_colorlist["darkkhaki"] = "#bdb76b";
	$_colorlist["darkmagenta"] = "#8b008b";
	$_colorlist["darkolivegreen"] = "#556b2f";
	$_colorlist["darkorange"] = "#ff8c00";
	$_colorlist["darkorchid"] = "#9932cc";
	$_colorlist["darkred"] = "#8b0000";
	$_colorlist["darksalmon"] = "#e9967a";
	$_colorlist["darkseagreen"] = "#8fbc8f";
	$_colorlist["darkslateblue"] = "#483d8b";
	$_colorlist["darkslategray"] = "#2f4f4f";
	$_colorlist["darkturquoise"] = "#00ced1";
	$_colorlist["darkviolet"] = "#9400d3";
	$_colorlist["deeppink"] = "#ff1493";
	$_colorlist["deepskyblue"] = "#00bfff";
	$_colorlist["dimgray"] = "#696969";
	$_colorlist["dodgerblue"] = "#1e90ff";
	$_colorlist["firebrick"] = "#b22222";
	$_colorlist["floralwhite"] = "#fffaf0";
	$_colorlist["forestgreen"] = "#228b22";
	$_colorlist["fuchsia"] = "#ff00ff";
	$_colorlist["gainsboro"] = "#dcdcdc";
	$_colorlist["ghostwhite"] = "#f8f8ff";
	$_colorlist["gold"] = "#ffd700";
	$_colorlist["goldenrod"] = "#daa520";
	$_colorlist["gray"] = "#808080";
	$_colorlist["green"] = "#008000";
	$_colorlist["greenyellow"] = "#adff2f";
	$_colorlist["honeydew"] = "#f0fff0";
	$_colorlist["hotpink"] = "#ff69b4";
	$_colorlist["indianred"] = "#cd5c5c";
	$_colorlist["indigo"] = "#4b0082";
	$_colorlist["ivory"] = "#fffff0";
	$_colorlist["khaki"] = "#f0e68c";
	$_colorlist["lavender"] = "#e6e6fa";
	$_colorlist["lavenderblush"] = "#fff0f5";
	$_colorlist["lawngreen"] = "#7cfc00";
	$_colorlist["lemonchiffon"] = "#fffacd";
	$_colorlist["lightblue"] = "#add8e6";
	$_colorlist["lightcoral"] = "#f08080";
	$_colorlist["lightcyan"] = "#e0ffff";
	$_colorlist["lightgoldenrodyellow"] = "#fafad2";
	$_colorlist["lightgreen"] = "#90ee90";
	$_colorlist["lightgrey"] = "#d3d3d3";
	$_colorlist["lightpink"] = "#ffb6c1";
	$_colorlist["lightsalmon"] = "#ffa07a";
	$_colorlist["lightseagreen"] = "#20b2aa";
	$_colorlist["lightskyblue"] = "#87cefa";
	$_colorlist["lightslategray"] = "#778899";
	$_colorlist["lightsteelblue"] = "#b0c4de";
	$_colorlist["lightyellow"] = "#ffffe0";
	$_colorlist["lime"] = "#00ff00";
	$_colorlist["limegreen"] = "#32cd32";
	$_colorlist["linen"] = "#faf0e6";
	$_colorlist["magenta"] = "#ff00ff";
	$_colorlist["maroon"] = "#800000";
	$_colorlist["mediumaquamarine"] = "#66cdaa";
	$_colorlist["mediumblue"] = "#0000cd";
	$_colorlist["mediumorchid"] = "#ba55d3";
	$_colorlist["mediumpurple"] = "#9370db";
	$_colorlist["mediumseagreen"] = "#3cb371";
	$_colorlist["mediumslateblue"] = "#7b68ee";
	$_colorlist["mediumspringgreen"] = "#00fa9a";
	$_colorlist["mediumturquoise"] = "#48d1cc";
	$_colorlist["mediumvioletred"] = "#c71585";
	$_colorlist["midnightblue"] = "#191970";
	$_colorlist["mintcream"] = "#f5fffa";
	$_colorlist["mistyrose"] = "#ffe4e1";
	$_colorlist["moccasin"] = "#ffe4b5";
	$_colorlist["navajowhite"] = "#ffdead";
	$_colorlist["navy"] = "#000080";
	$_colorlist["oldlace"] = "#fdf5e6";
	$_colorlist["olive"] = "#808000";
	$_colorlist["olivedrab"] = "#6b8e23";
	$_colorlist["orange"] = "#ffa500";
	$_colorlist["orangered"] = "#ff4500";
	$_colorlist["orchid"] = "#da70d6";
	$_colorlist["palegoldenrod"] = "#eee8aa";
	$_colorlist["palegreen"] = "#98fb98";
	$_colorlist["paleturquoise"] = "#afeeee";
	$_colorlist["palevioletred"] = "#db7093";
	$_colorlist["papayawhip"] = "#ffefd5";
	$_colorlist["peachpuff"] = "#ffdab9";
	$_colorlist["peru"] = "#cd853f";
	$_colorlist["pink"] = "#ffc0cb";
	$_colorlist["plum"] = "#dda0dd";
	$_colorlist["powderblue"] = "#b0e0e6";
	$_colorlist["purple"] = "#800080";
	$_colorlist["red"] = "#ff0000";
	$_colorlist["rosybrown"] = "#bc8f8f";
	$_colorlist["royalblue"] = "#4169e1";
	$_colorlist["saddlebrown"] = "#8b4513";
	$_colorlist["salmon"] = "#fa8072";
	$_colorlist["sandybrown"] = "#f4a460";
	$_colorlist["seagreen"] = "#2e8b57";
	$_colorlist["seashell"] = "#fff5ee";
	$_colorlist["sienna"] = "#a0522d";
	$_colorlist["silver"] = "#c0c0c0";
	$_colorlist["skyblue"] = "#87ceeb";
	$_colorlist["slateblue"] = "#6a5acd";
	$_colorlist["slategray"] = "#708090";
	$_colorlist["snow"] = "#fffafa";
	$_colorlist["springgreen"] = "#00ff7f";
	$_colorlist["steelblue"] = "#4682b4";
	$_colorlist["tan"] = "#d2b48c";
	$_colorlist["teal"] = "#008080";
	$_colorlist["thistle"] = "#d8bfd8";
	$_colorlist["tomato"] = "#ff6347";
	$_colorlist["turquoise"] = "#40e0d0";
	$_colorlist["violet"] = "#ee82ee";
	$_colorlist["wheat"] = "#f5deb3";
	$_colorlist["white"] = "#ffffff";
	$_colorlist["whitesmoke"] = "#f5f5f5";
	$_colorlist["yellow"] = "#ffff00";
	$_colorlist["yellowgreen"] = "#9acd32";

	$color = strtolower($color);
	if (empty($color))
	{
		return "#000000";
	} else if (isset($_colorlist[$color])) {
		return $_colorlist[$color];
	} else if (substr($color, 0, 1) == "#") {
		return $color;
	}

	return "#000000";
}

/**
* Returns santized data off CDATA etc
*/
function returnSanitizedMobileData($data)
{
	return str_replace("<![CDATA[", "&lt;![CDATA[", str_replace("]]>", "]]&gt;", $data));
}
?>